package com.example.module_word.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.example.module_word.R;
import com.example.module_word.data.DanCiData;
import com.example.module_word.listener.LearnOnClickItemListener;
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import java.util.List;

public class IndexStackRvAdapter extends RecyclerView.Adapter<IndexStackRvAdapter.ViewHolder> {
    private List<String> list;

    public IndexStackRvAdapter(List<String> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_index_stack, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //获取该书已学习的单词数量
        int pb = DanCiData.getMyWordCountByKind(list.get(position));
        int count = Math.toIntExact(DanCiData.getCountByKind(list.get(position)));
        holder.name.setText(list.get(position));
        holder.watch.setText((int) (Math.random() * 100000) + "人观看");
        holder.number.setText(pb + "/" + count);
        holder.progressBar.setProgress(pb);
        holder.progressBar.setMax(count);
        //查询是否已经保存在数据库，是则为true
        holder.isAdd.setSelected(DanCiData.isMyStackByKind(list.get(position)));
        holder.isAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //防抖
                if (QuickClickUtils.isFastClick()) {
                    return;
                }
                if (PublicFunction.checkCanNext()) {

                    holder.isAdd.setSelected(!(holder.isAdd.isSelected()));
                    //true则保存到我的书库，false则删除
                    if (holder.isAdd.isSelected()) {
                        DanCiData.insertMyStack(list.get(holder.getLayoutPosition()), false);
                    } else {
                        DanCiData.deleteMyStackByKind(list.get(holder.getLayoutPosition()));
                    }
                }
            }
        });

        holder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.isAdd.isSelected()) {
                    if (learnOnClickItemListener != null) {
                        learnOnClickItemListener.onItemClick(list.get(holder.getLayoutPosition()));
                    }
                } else {
                    ToastUtils.showShort("请先加入到我的书库");
                }
            }
        });
    }

    private LearnOnClickItemListener learnOnClickItemListener;

    public void getOnClick(LearnOnClickItemListener learnOnClickItemListener) {
        this.learnOnClickItemListener = learnOnClickItemListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ConstraintLayout bg;
        private TextView name, watch, number;
        private ImageView isAdd;
        private ProgressBar progressBar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            bg = itemView.findViewById(R.id.rv_index_stack_bg);
            name = itemView.findViewById(R.id.rv_index_stack_name);
            isAdd = itemView.findViewById(R.id.rv_index_stack_is_add);
            progressBar = itemView.findViewById(R.id.rv_index_stack_ProgressBar);
            watch = itemView.findViewById(R.id.rv_index_stack_watch);
            number = itemView.findViewById(R.id.rv_index_stack_number);
        }
    }
}
